/* Function for this class, main class for displayObject.
 * connect every element the chart diagram needed.
 * 
 * 
 */
package com.cf.core.model.diagram {
	import com.cf.core.model.diagram.background.axis.axisBase;
	import com.cf.core.view.MySprite;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	
	import com.cf.core.Utils.tools;
	import com.cf.core.Utils.debug;
	
	import com.cf.core.model.diagram.background.*;
	import com.cf.core.model.diagram.constants.*;
	import com.cf.core.model.diagram.config.*;
	public class diagram extends Sprite {
		private var G_layer00:Sprite;
		private var G_param:param;
		public function get PARAM():param { return G_param;}
		public function get BACKGROUND():*{ return this.PARAM.background || false; }
		private var G_chart:*;
		public function set CHART(_g):*{G_chart = _g }
		public function get CHART():*{ return G_chart }
		
		
		
		public function diagram():void{
			this.RELEASE();
			this.addEventListener(Event.ADDED_TO_STAGE, handleScene);
			this.addEventListener(Event.RESIZE, handleScene);
			this.addEventListener(Event.REMOVED_FROM_STAGE, handleScene);
		}//fn
		
		public function resize(_w:Number, _h:Number, _padding:Number=-1):void {
			// don't access width & height direct, it will cause the image adjust scale only.
			// but the graph of diagram will not be scale.
			// debug.msg("diagram : resize to width:"+ _w +" height:"+ _h +" padding:"+ _padding);
			this.PARAM.width = _w;
			this.PARAM.height = _h;
			try{
				if (_padding != -1) {
					this.BACKGROUND.padding = _padding;
				}//if
			}catch(e){}
			reload();
		}//fn
		public function draw(_prop:param):void {
			if ( _prop.ready ) {
				G_param = _prop;
			}else { debug.msg("diagram : Please input the chart's data 'param.data=[{},{},{}]'");}
			reload();
		}//fn
		public function reload():void {
			try {
				this.BACKGROUND.setup(this.PARAM);	// sync
			}catch (e) { debug.msg("diagram : Error PARAM pass to BACKGROUND fail."); }
			try {
				this.BACKGROUND.draw(); // <-- Error, function undefined...
				tools.emptySprite(G_layer00);
				G_layer00.addChild(this.BACKGROUND);
			}catch (e) { debug.msg("diagram : Error BACKGROUND draw fail."); }
		}//fn
		private function handleScene(e:Event):void {
			switch(e) {
				case Event.ADDED_TO_STAGE:
					
				break;
				case Event.RESIZE:
					debug.msg("I'm on change");
				break;
				case Event.REMOVED_FROM_STAGE:
					RELEASE();
				break; 
			}//case
		}//fn
		public function RELEASE(e:*= null):void {
			try { this.BACKGROUND.RELEASE(); } catch (ee) { }
			G_param = new param();
			G_layer00 = new Sprite();
			tools.emptySprite(this);	// clearup
			this.addChildAt(G_layer00, 0);
		}//fn
		override public function toString():String {
			return "com.cf.core.model.diagram.diagram";
		}//fn

	}//class
}//package